<template>
	<view>
		<search-bar @click="goSearch"></search-bar>
		<!-- 内容 -->
		<view class="content-box">
			<!-- 左边 -->
			<scroll-view class="left" scroll-y="true" :style="{height:wh+'px'}">
				<view :class="[current===index ? 'active' : '']" @click="changeCate(item,index)"
					v-for="(item,index) in CateList" :key="index">{{item.cat_name}}</view>
			</scroll-view>
			<!-- 右边 -->
			<scroll-view :scroll-top="scrolltop" class="right" scroll-y="true" :style="{height:wh+'px'}">
				<view v-for="(item,index) in CateList2" :key="index">
					<!-- 标题 -->
					<view class="title">/ {{item.cat_name}} /</view>
					<!-- 内容 -->
					<view class="content">
						<view class="content-item" v-for="(e,i) in item.children" :key="i" @click="goList(e)">
							<image :src="e.cat_icon"></image>
							<text>{{e.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				CateList: [],
				// 屏幕高度
				wh: 0,
				CateList2: [],
				current: 0,
				scrolltop: 0
			};
		},
		onLoad() {
			this.getCateList()
			// 获取设备屏幕的高度
			const res = uni.getSystemInfoSync() //同步
			console.log(res.windowHeight)
			this.wh = res.windowHeight - 50
		},
		methods: {
			// 跳转到搜索
			goSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
			// 跳转商品列表
			goList(item) {
				// console.log(item)
				uni.navigateTo({
					url: '/subpkg/goods-list/goods-list?cat_id=' + item.cat_id
				})
			},
			// 切换分类
			changeCate(item, index) {
				// console.log(item,index)
				// 切换高亮的样式
				this.current = index
				// 切换右侧的内容
				this.CateList2 = item.children
				this.scrolltop = this.scrolltop === 0 ? 0.1 : 0
			},
			// 获取分类列表
			async getCateList() {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/categories')
				console.log(message)
				if (meta.status !== 200) return uni.$showMsg()
				this.CateList = message
				this.CateList2 = message[0].children
			}
		}
	}
</script>

<style lang="scss">
	.content-box {
		display: flex;

		.left {
			width: 90px;

			view {
				font-size: 12px;
				line-height: 60px;
				text-align: center;
				background-color: #f7f7f7;
			}

			.active {
				background-color: #fff;
				position: relative;

				&::before {
					position: absolute;
					left: 0;
					top: 50%;
					margin-top: -15px;
					content: '';
					width: 2px;
					height: 30px;
					background-color: #ea4350;
				}
			}
		}

		.right {
			.title {
				font-size: 12px;
				font-weight: 700;
				text-align: center;
				margin: 6px 0;
			}

			.content {
				display: flex;
				flex-wrap: wrap;

				.content-item {
					width: calc(100%/3);
					display: flex;
					flex-direction: column;
					align-items: center;
					font-size: 12px;
					margin-bottom: 6px;

					image {
						width: 60px;
						height: 60px;
					}
				}
			}
		}
	}
</style>
